package com.rtx.wcs.ess.ristowcs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rtx.edi.platform.synctask.service.ConvertService;
import com.rtx.wcs.std.bean.po.RtxPalletcantilever;
import com.rtx.wcs.std.handle.ristowcs.rfctu.AbsRFCTUTaskDownHandle;
import com.rtx.wcs.std.service.RtxPalletcantileverService;


/**
 * RF-CTU换箱
 * @author Administrator
 *
 */
@Service("RF-CTU")
public class RFCTU extends AbsRFCTUTaskDownHandle{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	RtxPalletcantileverService rtxPalletcantileverService;
	
	@Value("${ess.task.down.url}")
	private String RF_CTU_URL;
	
	@Autowired
	ConvertService convertService;
 	
	@Override
	protected List<RtxPalletcantilever> queryToSendDataByRISWmsCondition(String request) {
		List<RtxPalletcantilever> list = super.queryToSendDataByRISWmsCondition(request);
		list.forEach(temp->temp.setTaskcode(UUID.randomUUID().toString()));
		return list;
	}


	//	{
	//		"code": 0,
	//		"msg": "success",
	//		"data": {
	//			"taskCode-001": "OK"
	//		}
	//	}
	@Override
	protected Map<String, String> handleResponse(RtxPalletcantilever o, String response,String mappingcode) {
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("editflag", "");//PS为成功 PE为失败
		resultMap.put("message", "");
		resultMap.put("data", "");
		resultMap.put("status", "");
		
		try {
			JSONObject res = JSON.parseObject(response);
			if("0".equals(res.getString("code"))) {
				resultMap.put("editflag", "PS");
			}else {
				resultMap.put("editflag", "PE");
			}
			resultMap.put("message", res.getString("msg"));
			resultMap.put("data", o.getTaskcode());
		} catch (Exception e) {
			logger.error("",e);
			resultMap.put("message", response);
			resultMap.put("editflag", "PE");
		}finally {
			resultMap.put("data", o.getTaskcode());
		}
		return resultMap;
	}



	//示例报文
//		{
//			"taskType": "putaway",
//			"taskGroupCode": "taskGroupCode-001",
//			"groupPriority": 0,
//			"tasks": [{
//				"taskCode": "taskCode-001",
//				"taskPriority": 0,
//				"taskDescribe": {
//					"containerCode": "Container001",
//					"containerType": "CT_KUBOT_STANDARD",
//					"storageTag": "",
//					"fromLocationCode": "S07-001-008-02",
//					"toStationCode": "Aport_station_1",
//					"toLocationCode": "A-001-001-01"
//				}
//			}]
//		}
	@Override
	protected JSONObject converWcsRequest(RtxPalletcantilever t, String mappingcode) {
		
		JSONObject edirequest = null;
		
		if(StringUtils.hasLength(mappingcode)) {
			edirequest = convertByConfig(t,mappingcode);
		}else {
			edirequest = convertByMapping(t);
		}
		return edirequest;
	}
	
	private JSONObject convertByConfig(RtxPalletcantilever t, String mappingcode){
		String source = JSON.toJSONString(t);
		String targetjson = convertService.ConvertByMappingCodeAndSource(mappingcode, source);
		return JSON.parseObject(targetjson);
	}
	
	private JSONObject convertByMapping(RtxPalletcantilever t){
		JSONObject edirequest = new JSONObject();
		edirequest.put("taskType", "putaway");
		edirequest.put("taskGroupCode", "");
		edirequest.put("groupPriority", "0");
		
		
		JSONArray tasks = new JSONArray();
		JSONObject task = new JSONObject();
		task.put("taskCode", t.getTaskcode());
		task.put("taskPriority", "0");
		
		JSONObject taskDescribe = new JSONObject();
		taskDescribe.put("containerCode", t.getLpn());
		taskDescribe.put("containerType", "CT_KUBOT_STANDARD");
		taskDescribe.put("storageTag", t.getPositioncode());
		taskDescribe.put("fromLocationCode", t.getStation());
		taskDescribe.put("toStationCode", "");
		taskDescribe.put("toLocationCode", t.getLoc());
		
		task.put("taskDescribe", taskDescribe);
		
		
		tasks.add(task);
		
		edirequest.put("tasks", tasks);
		
		return edirequest;
	}


	@Override
	protected String getEdiRequestURL(String mappingcode) {
		return RF_CTU_URL;
	}

}
